<template>
	<div class="seller-product-info-container">
		<el-card shadow="hover">
			<!-- 查询组件 start -->
			<div class="product-info-search mb15">
				<el-input size="small" placeholder="请输入商品名称" style="max-width: 180px"> </el-input>
				<el-button size="small" type="primary" class="ml10">
					<el-icon>
						<elementSearch />
					</el-icon>
					查询
				</el-button>
				<el-button size="small" type="success" class="ml10" @click="onOpenAddUser">
					<el-icon>
						<elementFolderAdd />
					</el-icon>
					新增商品
				</el-button>
			</div>
			<!-- 查询组件 end -->

			<!-- 数据展示 start -->
			<el-table :data="tableData.data" style="width: 100%; ">
				<!-- 商品的详细信息展示 start -->
				<el-table-column type="expand" >
					<template #default="props" >
						<p>商品编号: {{ props.row.productId }}</p>
						<p>商品名称: {{ props.row.productName }}</p>
						<p>商品价格: ￥{{ props.row.productPrice }}</p>
						<p>商品库存: {{ props.row.productStock }}</p>
						<p>商品状态: {{ props.row.productStatus == 0?"上架":"下架" }}</p> 
						<p>商品描述: {{ props.row.productDescription }}</p>
						<!-- <p>State: {{ props.row.productIcon }}</p> -->
						<p>类目编号: {{ props.row.categoryId }}</p>
						<p>商品类目: {{ props.row.categoryName }}</p>
						<p>创建时间: {{ props.row.createTime }}</p> 
					</template>
				</el-table-column>
				<!-- 商品的详细信息展示 end -->

				<el-table-column prop="productId" label="商品编号"></el-table-column>
				<el-table-column prop="productName" label="商品名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="productStock" label="商品库存" show-overflow-tooltip></el-table-column>
				<el-table-column prop="productPrice" label="商品价格" :formatter="formatMoney" show-overflow-tooltip></el-table-column>
				<el-table-column prop="productDescription" label="商品描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="productIcon" label="商品图片" show-overflow-tooltip>
					<template #default="scope">
						<el-avatar shape="square" :size="50" :src="scope.row.productIcon"></el-avatar>
						 <!-- <el-image  shape="square" :size="50" :src="scope.row.productIcon" style="width: 100px;height: 100px;"></el-image> -->
					</template>
				</el-table-column>
				<el-table-column prop="productStatus" label="商品状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.productStatus == 0"> 上架 </el-tag>
						<el-tag type="danger" v-else> 下架 </el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="categoryName" label="商品类目" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="mini" type="text" @click="onOpenEditUser(scope.row)">修改</el-button>
						<el-button size="mini" type="text" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>

				<!-- <el-table-column type="index" label="序号" width="50" />
				<el-table-column prop="userName" label="账户名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="userNickname" label="用户昵称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleSign" label="关联角色" show-overflow-tooltip></el-table-column>
				<el-table-column prop="department" label="部门" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="email" label="邮箱" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="describe" label="用户描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.userName === 'admin'" size="mini" type="text" @click="onOpenEditUser(scope.row)">修改</el-button>
						<el-button :disabled="scope.row.userName === 'admin'" size="mini" type="text" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column> -->
			</el-table>
			<!-- 数据展示 end -->

			<!-- 分页组件 start -->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNum"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
			<!-- 分页组件 end -->
		</el-card>
		<AddProductInfo ref="addProductRef" />
		<EditProductInfo ref="editProductRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref , computed, onUnmounted, getCurrentInstance} from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AddProductInfo from './component/addProductInfo.vue';
import EditProductInfo from './component/editProductInfo.vue';
import { useStore } from '/@/store/index';
import {getProInfoPage, deleteProduct} from '/@/api/product/product.ts';


// 测试数据
import { productInfoList } from '../mock.ts';

export default {
	name: 'productInfo',
	components: { AddProductInfo, EditProductInfo },
	setup() {
		const { proxy } = getCurrentInstance() as any;

		const store = useStore();
		const addProductRef = ref();
		const editProductRef = ref();
		const state: any = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
				},
			},
		});

		// 获取用户信息 vuex
		const getUserInfos = computed(() => {
			return store.state.userInfos.userInfos;
		});

		// 初始化表格数据
		const initTableData = () => {

			let sellerId :string = store.state.userInfos.userInfos.id;
			// let paramParam = {
			let	page :number =  state.tableData.param.pageNum;
			let	size :number = state.tableData.param.pageSize;
			// }
			// console.log(paramParam);
			getProInfoPage(page, size, sellerId).then((res:any) =>{
				console.log(res);
				state.tableData.param.pageNum = res.data.pageResult.current;
				state.tableData.total = res.data.pageResult.total;
				state.tableData.param.pageSize = res.data.pageResult.size;
				state.tableData.data = res.data.pageResult.records;

			})
		};
		// 打开新增用户弹窗
		const onOpenAddUser = () => {
			addProductRef.value.openDialog();
			// editUserRef.value.openDialogNoParam();
			// editUserRef.value.openDialog1();
		};
		// 打开修改用户弹窗
		const onOpenEditUser = (row: Object) => {
			editProductRef.value.openDialog(row);
		};
		// 删除用户
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除商品编号“${row.productId}”: “${row.productName}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteProduct(row.productId)
						.then((res:any) => {
							// console.log('这是商品信息数据' + productInfoList);
							ElMessage.success('删除成功');
							initTableData();
						})
						
					
				})
				.catch(() => {});
		};
		// 分页改变 页面大小改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
			initTableData();
		};
		// 分页改变 页面数改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
			initTableData();
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});

		// 格式化价格，在前添加￥
		const formatMoney = (val: any) => {
			return val.productPrice ? '￥' + val.productPrice : '￥0.00';
		};

		const options = [
			{
				value: 1,
				label: 'Option1',
			},
			{
				value: 2,
				label: 'Option2',
			},
			{
				value: 3,
				label: 'Option3',
			},
			{
				value: 4,
				label: 'Option4',
			},
			{
				value: 5,
				label: 'Option5',
			},
		];


		// 父子组件通信
		// 子父组件之间通信 start
		onMounted(() => {
			initTableData();
            // 收到
            proxy.mittBus.on('initTableData', initTableData)
        })
        onUnmounted(()=>{
            // 离开时销毁
            proxy.mittBus.off("initTableData")
		})
		return {
			options,
			formatMoney,
			addProductRef,
			editProductRef,
			onOpenAddUser,
			onOpenEditUser,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			...toRefs(state),
		};
	},
};
</script>

<style scoped lang="scss">
// .seller-product-info-container {
// }
</style>
